package com.linkedin.paldb;

import com.linkedin.paldb.api.*;

import java.io.File;
import java.util.concurrent.Executors;

import static com.linkedin.paldb.api.Configuration.LOAD_FACTOR;

/**
 * @Description TODO
 * @Author lay yongqian.lai@luckincoffee.com
 * @Date 2021/07/30 23:36
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        Configuration configuration = PalDB.newConfiguration();
//        configuration.set(LOAD_FACTOR, "0.99");
        try {
            String file = "C:\\Users\\yongq\\Desktop\\store%s.paldb";

            for (int index = 0; index < 3; index++) {
                int finalIndex = index;
                Executors.newFixedThreadPool(1).submit(() -> {
                    StoreWriter writer = PalDB.createWriter(new File(String.format(file, finalIndex)), configuration);

                    int num = 50000000;
                    System.out.println("writing...");

                    for (int i = 0; i < num; i++) {
                        writer.put(i, i);
                        if (i % 10000000 == 0) {
                            System.out.println(i);
                        }
                    }
                    System.out.println("writed...");

                    writer.close();
                    System.out.println("writer closed");

                    StoreReader reader = PalDB.createReader(new File(String.format(file, finalIndex)));
                    System.out.println("reading");

                    for (int i = 0; i < num; i++) {
                        reader.get(i);
                        if (i % 10000000 == 0) {
                            System.out.println(i);
                        }
                    }
                    System.out.println("readed...");
                    reader.close();
                    System.out.println("reader closed");
                });
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }

        Thread.sleep(Integer.MAX_VALUE);
    }
}
